<?php


namespace App\Http\Controllers\Admin;


use App\DTOS\Admin\EquipMakeDTO;
use App\DTOS\Admin\UserMemberDTO;
use App\Http\Controllers\Controller;
use App\Models\Game\Equip;
use App\Services\Game\EquipMakeService;
use App\Services\Game\UserMemberService;
use App\Services\Game\UserRoleService;
use App\Utils\ResponseUtil;

class EquipMakeController extends Controller
{
    private EquipMakeService $equipMakeService;
    private UserRoleService $userRoleService;
    private UserMemberService $userMemberService;

    /**
     * EquipMakeController constructor.
     * @param EquipMakeService $equipMakeService
     * @param UserRoleService $userRoleService
     * @param UserMemberService $userMemberService
     */
    public function __construct(EquipMakeService $equipMakeService, UserRoleService $userRoleService, UserMemberService $userMemberService)
    {
        $this->equipMakeService = $equipMakeService;
        $this->userRoleService = $userRoleService;
        $this->userMemberService = $userMemberService;
    }


    public function paginate()
    {

        $DTO = new EquipMakeDTO(request()->all());
        try {
            $paginate = $this->equipMakeService->paginate($DTO);
            $userMemberPaginate = $this->userMemberService->paginate(new UserMemberDTO(['limit'=>9999999]));
            $members = array2_only($userMemberPaginate->items(),['id','username']);
            $equips = Equip::all()->toArray();
            $data = $paginate->items();
            foreach ($data as &$datum) {
                 $member = collect($members)->where('id',$datum['memId'])->first();
                 $equip = collect($equips)->where('id',$datum['item_id'])->first();
                 $datum['memberName'] = data_get($member,'username');
                 $datum['equipName'] = data_get($equip,'name');
                 $datum['unique_id'] = (string)$datum['unique_id'];
            }
            $res = [];
            $res['code'] = 0;
            $res['data'] = $data;
            $res['count'] = $paginate->total();
            return ResponseUtil::json($res);
        }catch (\Exception $e){
            return ResponseUtil::error($e->getMessage());
        }
    }

}
